<?php
if(!defined("IN_PHPSCUP"))exit("hacking");
class Nav
{

    var $_config = array();//导航设置数据 

    var $_file = '';//导航数据文件
   	
    function __construct($filename = '',$lang = '')
    {
        $this->Nav($filename);
    }

    function Nav($filename = '',$lang = '')
    {        
        $this->_file = ROOT_PATH . '/data/nav/' . (!empty($filename)?$filename:$GLOBALS['SCup']->SCup_name) . '.' . (!empty($lang)?$lang:$GLOBALS['Lang']->_lang_tp) . '.nav.php';
    }

    function set_file($filename = '',$lang = '')
    {        
        $this->_file = ROOT_PATH . '/data/nav/' . (!empty($filename)?$filename:$GLOBALS['SCup']->SCup_name) . '.' . (!empty($lang)?$lang:$GLOBALS['Lang']->_lang_tp) . '.nav.php';
    }


    function _get_nav()//得到导航
    {   

		      if(is_file($this->_file))
		      {	    
		           $this->_config = require ($this->_file);
		      }
			  else
			  {
			       $this->_config = array();
			  }


		return $this->_config;   
    }


    //$postdata是提交过来的数据
    function _set_nav($postdata = array(),$name = '')//设置导航
    {   
	    if(empty($postdata))
		{
		     return false;
		}

        $this->_config = $this->_get_nav();
				
		array_push($this->_config,$postdata);
		
		array_var_sort($this->_config,$name);	
				  
	    write_file($this->_file, "<?php if(!defined('IN_PHPSCUP'))exit('hacking'); \nreturn " . var_export($this->_config , true) . ";\n?>");
		
		if(!is_writable($this->_file))
		{
		exit('file directory is not writed power');//文件目录权限不可写
	    }
		   
    
    }

    function curnav($istop=true)
    {
		$navigation = $this->_get_nav();
		if(!isset($_GET['app']))$_GET['app']='';
		if(!isset($_GET['act']))$_GET['act']='';
		if(!isset($_GET['cat']))$_GET['cat']='0';
		if(!isset($_GET['id']))$_GET['id']='0';	
		if(!isset($_GET['pa']))$_GET['pa']='';				
		$app = $_GET['app'];
	    $act = $_GET['act'];
	    $pa = $_GET['pa'];		
	    $cat = intval($_GET['cat']);		
	    $id = intval($_GET['id']);	
			
		$tempvalue = array();

		foreach($navigation as $key => $value)
		{ 
		   
		   if(!empty($value['link']) && $app  )
		   {
		       $link = str_replace(array('-'),array("="), $value['link']); 

			   if($app=='alonepage' )
			   {   
			       $id = $_GET['pa'];
			       if( strpos($link,'app='.$app) &&  strpos($link,'pa='.$id))
			       {
				   $tempvalue = $value;
				   }
				   
			   }else if($app=='new' || $app=='prot')
			   {
			     if($id && strpos($link,'app='.$app) &&  strpos($link,'act='.$act) &&  strpos($link,'id='.$id))
				 {
				   $tempvalue = $value;	
				 }else if($cat && strpos($link,'app='.$app) &&  strpos($link,'cat='.$cat))
				 {
				   $tempvalue = $value;	
				 }else if ( !$cat && strpos($link,'app='.$app) )
				 {
				   $tempvalue = $value;
				 }
			     	   
			   }else 
			   {
			        
			       if($act && strpos($link,'app='.$app) &&  strpos($link,'act='.$act)  )
				   {
				   $tempvalue = $value;
				   }else if (!$act && strpos($link,'app='.$app) )
				   {
				   $tempvalue = $value;
				   }
				 		 
			   }
		   }
		}
		if(!empty($tempvalue))
		{
		   $temp = array();
		   $bijiao = 0 ;
		   if($tempvalue['parent_id'] == 0)
		   {
		        $bijiao = $tempvalue['nav_id'] ;
		   }else
		   {
		   
		        if($istop)
				{
				 $tempvalue = $this->get_parent_id_array($navigation,$tempvalue['parent_id']);
				 $bijiao = $tempvalue['nav_id'] ;
				}else
				{
				  $bijiao = $tempvalue['parent_id'];
				}
				
		   }
		   foreach($navigation as $key => $value)
		   { 
			  if($value['parent_id'] == $bijiao)
			  {
				  $temp[$key] =  $value ;
			  }
		   }
		   $data = array();
		   $data['cur'] = $tempvalue['nav_id'];	
		   $data['data'] = $temp;		   
		   return $data ; 
		   
		}else
		{
		   $data['cur'] = 0;	
		   $data['data'] = $navigation;			
		  return $data ; 
		}
		
    }

     function get_parent_id_array($array,$parentid=0) 
	 {
	        foreach($array as $v) 
			{
		       if($v['nav_id'] == $parentid ) 
			   { 
			       if($v['parent_id'] != 0)
				   {
				    return $this->get_parent_id_array($array,$v['parent_id']);
				   }else
				   {
				    return $v;
				   }
		      }
	       }
	       return false;
    }


}


?>